package com.jml.zlhq.app.ui;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.AppConfig;
import com.jml.zlhq.app.URLs;
import com.jml.zlhq.app.service.HttpService;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.util.UIUtil.OnTagClick;
import com.loopj.android.http.TextHttpResponseHandler;

public class SearchActivity extends BaseActivity {
	@SuppressWarnings("unused")
	private LinkedList<String> historyItems,hotItems;
	@SuppressWarnings("unused")
	private LinkedList<View> historyViews,hotViews;
	private RelativeLayout history,hot;
	private EditText input;
	private String hisStr;
	private LinkedList<String> hisList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_activity);
		
		LinearLayout leftLay = (LinearLayout) findViewById(R.id.content_header_left_lay);
		LinearLayout rightLay = (LinearLayout) findViewById(R.id.content_header_right_lay);
		leftLay.setVisibility(View.GONE);
		rightLay.setOnClickListener(this);
		ImageView roghtImage1 = (ImageView) findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.GONE);
		TextView rightText = (TextView) findViewById(R.id.content_header_right_text);
		rightText.setText("取消");
		
		hisStr = AppConfig.getAppConfig(this).get("search_his");
		hisList = new LinkedList<String>();
		if(!StringUtil.isEmpty(hisStr)){
			String[] arr = hisStr.split("###");
			for (String str : arr) {
				hisList.add(str);
			}
		}
		
		
		input = (AutoCompleteTextView)findViewById(R.id.content_header_center_edit);
		input.setImeOptions(EditorInfo.IME_ACTION_SEARCH);  
		input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)){
					String instr = input.getText().toString();
					if(StringUtil.isEmpty(instr)){
						UIUtil.ToastMessage(activity, "搜索内容为空！");
						return false;
					}
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("key", instr);
					Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
					intent.putExtra("data", map);
					startActivity(intent);
					finish();
					//UIUtil.openActivity(SearchActivity.this, SearchResultActivity.class, data)
					//activity.setResult(Activity.RESULT_OK,intent);
					//activity.finish();
					return true;
				}
				return false;
			}
		});
		
		history = (RelativeLayout) findViewById(R.id.search_history);
		hot = (RelativeLayout) findViewById(R.id.search_hot);
		
		historyItems = new LinkedList<String>();
		hotItems = new LinkedList<String>();
		
		loadData(null);
	}

	@Override
	public void loadData(Map<String, String> param) {
		HttpService.getByText(URLs.MAIN_SEARC_HOT, null, new TextHttpResponseHandler() {
			@Override
			public void onSuccess(int arg0, Header[] arg1, String resultStr) {
				try {
					JSONObject result = new JSONObject(resultStr);
					JSONArray data = result.getJSONArray("data");
					if(result.getInt("status")==0){
						historyViews = UIUtil.autoTag(SearchActivity.this, hisList, "key", null, history, new OnTagClick() {
							@Override
							public void onTagClick(Map<String,String> v) {
								//Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
								//intent.putExtra("data", map);
								//startActivity(intent);
								Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
								intent.putExtra("data", (Serializable)v);
								startActivity(intent);
								finish();
								//activity.setResult(Activity.RESULT_OK,intent);
								//activity.finish();
							}
						});
						hotViews = UIUtil.autoTag(SearchActivity.this, data, "key", null, hot, new OnTagClick() {
							@Override
							public void onTagClick(Map<String,String> v) {
								Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
								intent.putExtra("data", (Serializable)v);
								startActivity(intent);
								finish();
								//activity.setResult(Activity.RESULT_OK,intent);
								//activity.finish();
							}
						});
					}else{
						
					}
					
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
				UIUtil.ToastMessage(activity, "抱歉，访问失败");
			}
		});
		
		
	}

	@Override
	public void clearData() {

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.content_header_right_lay:
			finish();
			break;
		default:
			break;
		}
	}

}
